<template>
    <div>
        <p>
            <button v-show="hasResource('010101')" v-on:click="add" class="btn btn-white btn-info btn-round"  >
                <i class="ace-icon fa  fa-pencil-square-o"></i>
                新增
            </button>
            &nbsp;
            <button v-on:click="list(1)" class="btn btn-white btn-info btn-round"  >
                <i class="ace-icon fa fa-refresh"></i>
                刷新
            </button>
        </p>
        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                <th>用户名</th>
                <th>昵称</th>
                <th>密码</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="user in users">
                <td>{{user.loginName}}</td>
                <td>{{user.name}}</td>
                <td>{{user.password}}</td>
                <td>
                    <div  class="hidden-sm hidden-xs btn-group">
                        <button v-show="hasResource('010103')"  v-on:click="editPassword(user)" class="btn btn-xs btn-info">
                            <i class="ace-icon fa fa-key bigger-120"></i>
                        </button>

                        <button v-on:click="edit(user)" class="btn btn-xs btn-info">
                            <i class="ace-icon fa fa-pencil bigger-120"></i>
                        </button>

                        <button v-show="hasResource('010102')" v-on:click="del(user.id)"  class="btn btn-xs btn-danger">
                            <i class="ace-icon fa fa-trash-o bigger-120"></i>
                        </button>
                    </div>

                    <div class="hidden-md hidden-lg">
                        <div class="inline pos-rel">
                            <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                            </button>

                            <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                <li>
                                    <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
                                                                    <span class="blue">
                                                                        <i class="ace-icon fa fa-search-plus bigger-120"></i>
                                                                    </span>
                                    </a>
                                </li>

                                <li>
                                    <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
                                                                    <span class="green">
                                                                        <i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
                                                                    </span>
                                    </a>
                                </li>

                                <li>
                                    <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
                                                                    <span class="red">
                                                                        <i class="ace-icon fa fa-trash-o bigger-120"></i>
                                                                    </span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
        <pagination ref="pagination" v-bind:list="list" v-bind:itemCount="5"></pagination>
        <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">数据表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-10">
                                    <input v-model="user.loginName" v-bind:disabled="user.id" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵称</label>
                                <div class="col-sm-10">
                                    <input v-model="user.name" class="form-control">
                                </div>
                            </div>
                            <div v-show="!user.id"  class="form-group">
                                <label class="col-sm-2 control-label">密码</label>
                                <div class="col-sm-10">
                                    <input v-model="user.password" type="password" class="form-control">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button v-on:click="save()" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
        <div id="edit-password-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">修改密码</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-sm-2">密码</label>
                                <div class="col-sm-10">
                                    <input class="form-control" type="password" v-model="user.password" name="password">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-white btn-default btn-round" data-dismiss="modal">
                            <i class="ace-icon fa fa-times"></i>
                            取消
                        </button>
                        <button type="button" class="btn btn-white btn-info btn-round" v-on:click="savePassword()">
                            <i class="ace-icon fa fa-plus blue"></i>
                            保存密码
                        </button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
    </div>
</template>
<script>
    import Pagination from "../../components/pagination";
    export default {
        components: {Pagination},
        name: "system-user",
        mounted: function () {
            let _this = this;
            _this.$refs.pagination.size = 5;
            _this.list(1);
        },
        data:function(){
            return {
                users:[],
                user: {},
            }
        },
        methods:{
            /**
             * 列表查询
             */
            list(page) {
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER+'/system/user/getAllUsers', {
                    page: page,
                    size: _this.$refs.pagination.size
                }).then((response)=>{
                    let resp = response.data;
                    _this.users = resp.list;
                    _this.$refs.pagination.render(page, resp.total);
                })
            },
            /**
             * 点击【新增】弹出窗口
             */
            add() {
                let _this = this;
                _this.user = {};
                $("#form-modal").modal("show");
            },
            /**
             * 点击【保存】按钮
             */
            save() {
                let _this = this;
                _this.user.password = hex_md5(_this.user.password + KEY);
                _this.$ajax.post(process.env.VUE_APP_SERVER+'/system/user/save', _this.user).then((response)=>{
                    let resp = response.data;
                    if (resp.success) {
                        $("#form-modal").modal("hide");
                        _this.list(1);
                        Toast.success(resp.msg);
                    } else {
                        Toast.warning(resp.msg)
                    }
                });
            },
            /**
             * 点击【编辑】
             */
            edit(user) {
                let _this = this;
                _this.user = $.extend({}, user);
                $("#form-modal").modal("show");
            },
            /**
             * 点击【删除】
             */
            del(id) {
                let _this = this;
                Confirm.show("删除用户后不可恢复，确认删除？", function () {
                    _this.$ajax.get(process.env.VUE_APP_SERVER+'/system/user/deleteUser?id=' + id).then((response)=>{
                        let resp = response.data;
                        if (resp.success) {
                            _this.list(1);
                            Toast.success(resp.msg);
                        }
                    })
                });
            },
            /**
             * 点击【重置密码】
             */
            editPassword(user) {
                let _this = this;
                _this.user = $.extend({}, user);
                _this.user.password = null;
                $("#edit-password-modal").modal("show");
            },
            /**
             * 点击【保存密码】
             */
            savePassword() {
                let _this = this;

                _this.user.password = hex_md5(_this.user.password + KEY);//密码加密
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/user/save-password', _this.user).then((response)=>{
                    let resp = response.data;
                    if (resp.success) {
                        $("#edit-password-modal").modal("hide");
                        _this.list(1);
                        Toast.success(resp.msg);
                    } else {
                        Toast.warning(resp.msg)
                    }
                })
            },
            //过滤权限
            hasResource(id){
                return Tool.hasResource(id);
            }
        }
    }
</script>
